package com.epoint.BookManager.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.epoint.BookManager.dao.borrowInfoDao;
import com.epoint.BookManager.domain.BorrowInfo;
import com.epoint.BookManager.service.BorrowInfoService;


@WebServlet("/borrowinfolistaction")
public class BorrowInfoListAction extends HttpServlet
{
    private static final long serialVersionUID = 1L;


    BorrowInfoService borrowInfoService = new BorrowInfoService();
    public BorrowInfoListAction() {
        super();
    }

  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String method = request.getParameter("method");
        if ("showborrinfolist".equals(method)) {
            showBorrowInfoList(request, response);
        }
        else if ("querybookname".equals(method)) {
           // queryBookName(request, response);
        }
        else if ("returnbook".equals(method)) {
            try {
                returnBook(request, response);
            }
            catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else if ("deleteborrowinfo".equals(method)) {
            deleteBorrowInfo(request, response);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

    /**
     * 展现搜索列表
     * @param request
     * @param response
     * @throws IOException
     */
    private void showBorrowInfoList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String sortField = request.getParameter("sortField");
        String sortOrder = request.getParameter("sortOrder");
        List<BorrowInfo> bookList = borrowInfoService.getBookList(pageIndex, pageSize, sortField, sortOrder);
        Map<String ,Object> result = new HashMap<>();
		result.put("data",bookList );
		result.put("total",borrowInfoService.getBookList(pageIndex, Integer.MAX_VALUE, sortField, sortOrder).size());
		response.getWriter().print(JSONObject.toJSONStringWithDateFormat(result,  "YYYY-MM-dd HH:mm:ss"));
    }



    /**
     * 归还书籍
     * @param request
     * @param response
     * @throws IOException
     * @throws ParseException
     */

    private void returnBook(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        String borrowid = request.getParameter("borrowid");
        String bookid = request.getParameter("bookid");
        boolean flag = borrowInfoService.returnBook(borrowid, bookid);
        Map<String,String> resultMap = new HashMap<>();
        if(flag==true) {
        	resultMap.put("msg","还书成功");
        }else {
        	resultMap.put("msg","还书失败");
        }
      response.getWriter().print(JSONUtils.toJSONString(resultMap));
    };

    /**
     * 删除借阅信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteBorrowInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String borrowid = request.getParameter("borrowid");
        boolean flag = borrowInfoService.deleteBorrowInfo(borrowid);
        Map<String,String> resultMap = new HashMap<>();
        if(flag==true) {
        	resultMap.put("msg","删除成功");
        }else {
        	resultMap.put("msg","删除失败");
        }
      response.getWriter().print(JSONUtils.toJSONString(resultMap));
    };

}
